package com.thinkis.modules.app.api.common;

import cn.hutool.core.lang.Console;
import com.google.common.collect.Maps;
import com.thinkis.modules.app.annotation.Login;
import com.thinkis.modules.app.annotation.LoginUser;
import com.thinkis.modules.app.interceptor.AuthorizationInterceptor;
import com.thinkis.modules.app.util.R;
import com.thinkis.modules.sys.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * APP测试接口
 * @author liuzhiping
 * @date 2018年4月16日17:03:42
 */
@Controller
@RequestMapping("${apiPath}/v1/api")
@Api("APP测试接口")
public class AppTestController {

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ApiOperation("测试接口")
    @ResponseBody
    public R test(){
        return R.ok("测试成功");
    }

    @Login
    @RequestMapping(value = "test1",method = RequestMethod.GET)
    @ApiOperation("需要登录才能访问的接口")
    @ResponseBody
    public R test1(HttpServletRequest request){
        String userId = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        Console.log(userId);
        return R.ok();
    }

    @RequestMapping(value = "test2",method = RequestMethod.GET)
    @ApiOperation("测试接口2")
    @ResponseBody
    public R test2(){
        Map<String ,Object> map = Maps.newHashMap();
        map.put("data","测试");
        return R.ok(map);
    }

    @Login
    @RequestMapping(value = "userInfo" ,method = RequestMethod.GET)
    @ApiOperation("获取用户信息")
    @ResponseBody
    public R userInfo(@LoginUser User user){
        Console.log(user);
        return R.ok().put("user", user);
    }
}
